﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NAnt.Core;
using NAnt.Core.Types;
using NAnt.Core.Attributes;

namespace NAntExt.Functions
{
    /// <summary>
    /// NAnt Functions for <see cref="System.DateTime"/>.
    /// </summary>
    [FunctionSet("datetime", "DateTime")]
    public class DateTimeFunctions : FunctionSetBase
    {
        #region Public Instance Constructors

        public DateTimeFunctions(Project project, PropertyDictionary properties)
            : base(project, properties)
        {
        }

        #endregion Public Instance Constructors
        
        /// <summary>
        /// Subtracts a date and time from the another date and time.
        /// </summary>
        /// <param name="from">The from date</param>
        /// <param name="date">The to date</param>
        /// <returns>Returns the duration between the two dates.</returns>
        [Function("subtract-datetime")]
        public static TimeSpan SubtractDate(DateTime from, DateTime to)
        {
            return from.Subtract(to);
        }

        /// <summary>
        /// Subtracts a duration from a date and time.
        /// </summary>
        /// <param name="from">The date and time to start at.</param>
        /// <param name="length">The duration of time to subject.</param>
        /// <returns>The date and time which falls a specific duration behind the from date and time.</returns>
        [Function("subtract-timespan")]
        public static DateTime SubtractDuration(DateTime from, TimeSpan length)
        {
            return from.Subtract(length);
        }

        /// <summary>
        /// Formats a date and time into a specific format.
        /// </summary>
        /// <param name="value">The date and time value</param>
        /// <param name="format">The format specification</param>
        /// <returns>Returns the date and time formatted with the format specification.</returns>
        [Function("to-string")]
        public static string ToString(DateTime value, string format)
        {
            return value.ToString(format);
        }
    }
}
